package cn.edu.scau.cmi.crawler.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;
import javax.persistence.*;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;

import cn.edu.scau.cmi.ema.dao.AbstractJpaDao;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil; 
import java.math.BigDecimal;
import java.util.Calendar;

@Repository
@Transactional
public class ScauCmiCrawlerdetectDAOImpl extends AbstractJpaDao<Crawlerdetect> implements ScauCmiCrawlerdetectDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Crawlerdetect.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public ScauCmiCrawlerdetectDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}


	public boolean canBeMerged(Crawlerdetect entity) {
		return true;
	}

	@Override
	@Transactional
	public Set<Crawlerfile> findExcelByIsinsertcrawlereventOrdetectable(String isinsertcrawlereventordetectable)
			throws DataAccessException {

		return findExcelByIsinsertcrawlereventordetectable(isinsertcrawlereventordetectable, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfile> findExcelByIsinsertcrawlereventordetectable(String isinsertcrawlereventordetectable,
			int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findExcelByIsinsertcrawlereventordetectable", startResult, maxRows,
				isinsertcrawlereventordetectable);
		return new LinkedHashSet<Crawlerfile>(query.getResultList());
	}

	@Override
	public Set<Crawlerfile> findDetectCrawlerfileByFiletypeAndImportdetectstatus(Integer filetypeId, Integer filestatustypeId, int startResult,	int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findDetectCrawlerfileByFiletypeAndImportdetectstatus", startResult, maxRows, filetypeId, filestatustypeId);
		return new LinkedHashSet<Crawlerfile>(query.getResultList());
	}

	@Override
	public Set<Crawlerfile> findDetectCrawlerfileByFiletypeAndImporteventstatus(Integer filetypeId,
			String importrelationstatus, int startResult, int maxRows) {
		Query query = createNamedQuery("findDetectCrawlerfileByFiletypeAndImporteventstatus", startResult, maxRows, filetypeId, importrelationstatus);
		return new LinkedHashSet<Crawlerfile>(query.getResultList());
	}

	@Override
	public Set<Crawlerfile> findDetectCrawlerfileByNullExistStatus(int startResult, int maxRows) {
		Query query = createNamedQuery("findDetectCrawlerfileByNullExistStatus", startResult, maxRows);
		return new LinkedHashSet<Crawlerfile>(query.getResultList());
	}

	@Override
	public Set<Crawlerfile> findCrawlerfileByFiletypeExistCrawlerfilestatus(int filetypeId, boolean exist, int importstatustypeId,  int startResult, int maxRows) {
		Query query = createNamedQuery("findCrawlerfileByFiletypeExistCrawlerfilestatus", startResult, maxRows, filetypeId, exist, importstatustypeId);
		return new LinkedHashSet<Crawlerfile>(query.getResultList());
	}


}